#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 1e4 + 10, M = 105;
int n, m;
int a[N][M];
vector<int>alls;
int tree[M * N];


int lowbit(int x)
{
	return x & -x;
}

void add(int x, int v)
{
	for (int i = x; i <= alls.size(); i += lowbit(i)) {
		tree[i] += v;
	}
}

LL query(int x)
{
	LL ans = 0;
	for (int i = x; i; i -= lowbit(i)) {
		ans += tree[i];
	}
	return ans;
}
int find(int x)
{
	int l = -1, r = alls.size() + 1;
	while (l + 1 < r) {
		int mid = l + r >> 1;
		if (alls[mid] < x)l = mid;
		else r = mid;
	}
	return r;
}


int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> a[i][j];
			alls.push_back(a[i][j]);
		}
	}
	sort(alls.begin(), alls.end());
	alls.erase(unique(alls.begin(), alls.end()), alls.end());
	LL ans = 0;
	for (int i = n; i >= 1; i--) {
		int sum = 0;
		for (int j = 1; j <= m; j++) {
			int idx = find(a[i][j]) + 1;
			//cout << "idx==" << idx << endl;
			sum += query(idx);
		}
		//cout << "sum==" << sum << endl;
		LL temp = (1 + m) * m / 2;
		ans += temp * (i - 1) + sum;
		for (int j = 1; j <= m; j++) {
			int idx = find(a[i][j]) + 1;
			add(idx, 1);
		}
	}
	cout << ans << endl;
	return 0;
}